<?php

//
function csv2SQL_init($MapID) {
//
    if ($MapID) {
        $ColType = array();
        $SQL = "SELECT * FROM csv_map WHERE ID = '" . $MapID . "'";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);
        if ($result) {
            if ($num_rows > 0) {
                $MapRec = mysql_fetch_assoc($result);
            }
            $csvFile = $MapRec['csvFile'];
            $TableName = $MapRec['TableName'];
            $csvCol = explode(",", $MapRec['csvCol']);
            $MapTo = explode(",", $MapRec['MapTo']);
            $TableCol = explode(",", $MapRec['TableCol']);
            $UserExit = explode(",", $MapRec['UserExits']);
            $cCount = count($TableCol);
            $DoMap = array();
            for ($c = 0; $c < $cCount; $c++) {
                if ($MapTo[$c]) {
                    $DoMap[$MapTo[$c]] = true;
                }
            }
            $SQL = "INSERT INTO `" . $TableName . "` ";
            //$SQL = $SQL . " WHERE `ID` = '" . $field[0] . "'";
            $SQL_Cols = "";
            $SQL_Vals = "";
//
            for ($c = 0; $c < $cCount; $c++) {
                $ColType[$c] = "";
                //print " | " . $DoMap[$c] . "-" . $UserExit[$c];
                if ($DoMap[$c] || $UserExit[$c] <> "") {
                    if ($SQL_Cols) {
                        $SQL_Cols = $SQL_Cols . ", ";
                        $SQL_Vals = $SQL_Vals . ", ";
                    }
                    $SQL_Cols = $SQL_Cols . " `" . $TableCol[$c] . "`";
                    $SQL_Vals = $SQL_Vals . " '" . "field" . $c . "'";
                    //
                    $SQL2 = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'PPI' AND TABLE_NAME = '$TableName' AND COLUMN_NAME = '$TableCol[$c]'";
                    $result2 = mysql_query($SQL2);
                    $num_rows = mysql_num_rows($result2);
                    //
                    if ($result2) {
                        if ($num_rows > 0) {
                            $db_field = mysql_fetch_assoc($result2);
                            $ColType[$c] = $db_field['DATA_TYPE'];
                            //print "<br>". $c . ". " . $TableCol[$c] . " - " . $ColType[$c];
                        }
                    }
                }
            }
            $SQL = $SQL . " (" . $SQL_Cols . ")";
            $MapRec['DataType'] = implode(",", $ColType);
            $rarr = array();
            $rarr[0] = $MapRec;
            $rarr[1] = $SQL;
            return $rarr;
        }
    }
}

//
function csv2SQL_vals($MapID, &$MapRec, $dlim, &$Data) {
//
    //print "<p>";
    //var_dump($MapRec);
    //print "<p>";
    $UE_Func = $MapID;
    $UE_Code = $MapID . ".php";
    $TableName = $MapRec['TableName'];
    $csvCol = explode(",", $MapRec['csvCol']);
    $MapTo = explode(",", $MapRec['MapTo']);
    $TableCol = explode(",", $MapRec['TableCol']);
    $UserExit = explode(",", $MapRec['UserExits']);
    $ColType = explode(",", $MapRec['DataType']);
    $cCount = count($TableCol);
    $csvCount = count($csvCol);
    $DoMap = array();
    for ($c = 0; $c < $csvCount; $c++) {
        //print $MapTo[$c] . ", ";
        if ($MapTo[$c] <> "") {
            $DoMap[$MapTo[$c]] = $c;
        }
    }
    $Trace = $_SESSION['Work']['Trace'];
//
    $SQL_Vals = "";
    print "<br>";
    //var_dump($ColType);
    //print "<br>";
    for ($c = 0; $c < $csvCount; $c++) {
        //if ($TableCol[$c]) {
        //
        if ($DoMap[$c] || $UserExit[$c] <> "") {
            if ($SQL_Vals) {
                $SQL_Vals = $SQL_Vals . ", ";
            }
            if ($Data[$DoMap[$c]]) {
                $Val = " '" . $Data[$DoMap[$c]] . "'";
                if (strpos($UserExit[$c],"E") !== false) {
                    //print "<br> *E* " . $TableCol[$c] . " : " . $Val;
                    if (is_file($UE_Code)) {
                        $UE_Opts['Exit'] = "E";
                        $Val = $UE_Func($MapRec, $Data, $c, $UE_Opts);
                        if (!$Val) {
                            $Val = "null";
                        } else {
                            $Val = "'" . $Val . "'";
                        }
                    }
                }
                $date = strpos($ColType[$c], "date");
                //print "date=" . $date . ".";
                if ($date !== false && $Val !== "null") {
                    $Val = "STR_TO_DATE(" . $Val . ",'%d/%m/%Y')";
                }
            } else {
                $Val = "null";
            }
            if (strpos($UserExit[$c],"F") !== false) {
                //print "<br> *F* " . $TableCol[$c] . " : " . $Val;
                //print " *F*";
                if (is_file($UE_Code)) {
                    $UE_Opts['Exit'] = "F";
                    $Val = $UE_Func($MapRec, $Data, $c, $UE_Opts);
                    //print " val returned=" . $Val;
                    if (!$Val) {
                        $Val = "null";
                    } else {
                        $Val = "'" . $Val . "'";
                    }
                }
            }
            $SQL_Vals = $SQL_Vals . $Val;
        }
    }
    $SQL_Vals = " VALUES (" . $SQL_Vals . ")";
    return $SQL_Vals;
}
?>
